package org.trinity.netdisk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.trinity.netdisk.enums.ResultCode;
import org.trinity.netdisk.enums.ShareDays;
import org.trinity.netdisk.service.IShareFileService;
import org.trinity.netdisk.util.JWTUtil;
import org.trinity.netdisk.util.ResponseWrapper;
import org.trinity.netdisk.vo.shareFile.ShareFileLinkedCodeVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("unused")
@RequestMapping("/s")
@Controller
public class ShareFileController {
    @Autowired
    IShareFileService shareFileService;

    @RequestMapping("/getLink/{userFileId}/{shareDays}")
    @ResponseBody
    private Object getShareFileLink(@PathVariable("userFileId") Long userFileId, @PathVariable("shareDays") Integer shareDays, HttpServletRequest request) {
        Long userId = JWTUtil.getUserId(request);
        ShareFileLinkedCodeVO shareFileUrl = shareFileService.getShareFileUrl(userId, userFileId, ShareDays.integerToShareDays(shareDays));
        return new ResponseWrapper(ResultCode.SUCCESS, shareFileUrl);
    }

    @RequestMapping("/s/{shareFileId}")
    private String toDownloadPage() {
        return "/shareFile.html";
    }

    @RequestMapping("/info/{shareFileId}")
    @ResponseBody
    private Object getShareFileDetailInfo(@PathVariable("shareFileId") String shareFileId) {
        return shareFileService.getShareFileDetailInfo(shareFileId);
    }

    @RequestMapping("/isCorrectCheckCode/{shareFileId}/{checkCode}")
    @ResponseBody
    private Object checkShareCode(@PathVariable("shareFileId") String shareFileId, @PathVariable("checkCode") String checkCode) {
        shareFileService.checkShareCode(shareFileId, checkCode);
        return new ResponseWrapper(ResultCode.SUCCESS);
    }

    @RequestMapping("/d/{shareFileId}/{checkCode}")
    @ResponseBody
    private void download(@PathVariable("shareFileId") String shareFileId, @PathVariable("checkCode") String checkCode, @RequestHeader HttpHeaders httpHeaders, HttpServletResponse response) {
        try {
            shareFileService.download(httpHeaders, response, shareFileId, checkCode);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("/list")
    @ResponseBody
    private Object list(HttpServletRequest request){
        Long userId = JWTUtil.getUserId(request);
        return new ResponseWrapper(ResultCode.SUCCESS, shareFileService.selectShareVO(userId));
    }

    @RequestMapping("/delete/{shareId}")
    @ResponseBody
    private Object delete(HttpServletRequest request, @PathVariable("shareId") Long shareId){
        Long userId = JWTUtil.getUserId(request);
        shareFileService.delete(userId, shareId);
        return new ResponseWrapper(ResultCode.SUCCESS);
    }
}
